#include "CPlayerJumpState.h"
#include "CPlayerFallState.h"
#include "CPlayer.h"
#include "CTimer.h"
#include "CSGD_DirectInput.h"
#include "CSGD_FModManager.h"
#include "CGamePlayState.h"
//Gravity fall speed
#define GRAVITY 20
#include "testenum.h"

CPlayerJumpState::CPlayerJumpState()
{
	m_vAnimations.push_back(CAnimationEngine::GetInstance()->LoadAnimation(Greek_Jump));
	m_vAnimations.push_back(CAnimationEngine::GetInstance()->LoadAnimation(MidEvil_Jump));
	m_vAnimations.push_back(CAnimationEngine::GetInstance()->LoadAnimation(Gothic_Jump));
}

CPlayerJumpState *CPlayerJumpState::GetInstance()
{
	static CPlayerJumpState Instance;
	return &Instance;
}

void CPlayerJumpState::Enter(CPlayer *pPlayer)
{
	m_pPlayer = pPlayer;

#if LOADSFX
	// play the jump sound as long as its not already playing
	m_pFMOD->StopSound( m_pGPS->m_nSFX[ m_pGPS->JUMP_SFX ] );
	m_pFMOD->PlaySound( m_pGPS->m_nSFX[ m_pGPS->JUMP_SFX ] );
	m_pFMOD->SetVolume( m_pGPS->m_nSFX[ m_pGPS->JUMP_SFX ], m_pGPS->m_fJumpVolume );
#endif LOADSFX

}

void CPlayerJumpState::Run()
{
	m_vAnimations[m_pPlayer->m_unTimePeriod]->Play(CTimer::GetInstance()->GetElapsedTime());

	Vec3 DownVec = -m_pPlayer->m_Pos;
	DownVec.Normalize();

	Vec3 vFormerGravity = Dot(DownVec, m_pPlayer->m_Vel) * DownVec;

	CSGD_DirectInput * di = CSGD_DirectInput::GetInstance();
	if(di->KeyDown(DIK_RIGHTARROW))
	{
		m_pPlayer->m_bFlipped = false;
		
		Vec3 Right(m_pPlayer->m_Pos.y, -m_pPlayer->m_Pos.x, 0);
		Right.Normalize();
		Right *= m_pPlayer->m_fWalkSpeed;

		m_pPlayer->m_Vel = Right + vFormerGravity;	
	}
	else if (di->KeyDown(DIK_LEFTARROW))
	{
		m_pPlayer->m_bFlipped = true;

		Vec3 Left(-m_pPlayer->m_Pos.y, m_pPlayer->m_Pos.x, 0);
		Left.Normalize();
		Left *= m_pPlayer->m_fWalkSpeed;

		m_pPlayer->m_Vel = Left + vFormerGravity;
	}
	
	
	if (Dot(DownVec, m_pPlayer->m_Vel) < 0)
	{
		m_pPlayer->ChangeState(CPlayerFallState::GetInstance());
	}
	DownVec *= GRAVITY;		//Gravity Constant
	m_pPlayer->m_Vel += DownVec * CTimer::GetInstance()->GetElapsedTime();
}

void CPlayerJumpState::Exit()
{
}